package utils;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Account {
    private String cardId;
    private double money;
    
    private final Lock lk = new ReentrantLock();

    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }
//    synchronized
    public  void drawMoney(double money){
        String name = Thread.currentThread().getName();
//        synchronized (this) {
//        Condition condition = lk.newCondition();
//        condition.signalAll();
//        condition.await();
//        condition.notifyAll();
        try {
            lk.lock();
            if (this.money >= money){
                System.out.println(name + "取款成功" + money);
                this.money -= money;
                System.out.println(name + "剩余余额" + this.money);
            }else {
                System.out.println(name + "余额不足");
            }
            Thread.sleep(2000);
//        }
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            
            lk.unlock();
        }
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "cardId='" + cardId + '\'' +
                ", money=" + money +
                '}';
    }
}
